
# Memento Cache Proxy for Bing search engine.
# Author: Ahmed AlSum aalsum@cs.odu.edu
# Date: July 21, 2010

import sys, os, re
import urllib
import urllib2
import simplejson
from dateutil import parser as dateparser
from baseHandler import *

app_id = 'A50C44A313EF48C4CF9BEEB2B05BF7B6A4BE2E2D'
end_point = 'http://api.bing.net/json.aspx?Appid=%s&'%app_id


class BingHandler(BaseProxyHandler):

    def fetch_changes(self, req, requri, dt=None):

        # implement the changes list for this particular proxy

        payload={}
        #payload['Appid'] = self.app_id
        payload['query'] = 'url:'+requri
        payload['sources'] = 'web'

        payload['Version'] = '2.1'
        payload['Market'] = 'en-us'
        payload['Adult'] = 'Moderate'
        payload['Options'] = 'EnableHighlighting'
        payload['Web.Count'] = '10'
        payload['Web.Offset'] = '0'

        query_string = urllib.urlencode(payload)
        final_url = end_point + query_string

        try:
            response = urllib.urlopen(final_url)
        except:
            return self.error("Couldn't retrieve data from %s" % requri, req, status=404)

        # @type data dict
        data = simplejson.load(response)

        changes = None
      
        rlist = data['SearchResponse']
        webObj = rlist['Web']
        total = webObj['Total']
        if total > 0:
            results = webObj['Results']
            if len(results) > 0:
                result = results[0]

                # @type result dict
                try:
                    dtstr = result['DateTime']
                    if result.has_key('CacheUrl'):

                        locEncoding =  result['CacheUrl']
                        # @type locEncoding str
                        loc = str(locEncoding)
                        loc = loc.replace('?q','?d')
                        if check_cache_availability(loc):
                            changes = []
                            dtobj = dateparser.parse(dtstr)
                            changes.append((dtobj, loc, {'last' : dtobj, 'obs' : 1, 'type' : 'observed'}))

                except:
                   return self.error("Error in parsing Bing search results for  %s" % requri, req, status=500)

        return changes

def check_cache_availability(cacheUri):
    user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.10) Gecko/20100504 Firefox/3.5.10 (.NET CLR 3.5.30729)'

    headers = {'User-Agent': user_agent}

    request = urllib2.Request(cacheUri, None, headers)

    opener = urllib2.build_opener()
    response = opener.open(request)

    the_page = response.read()


    unfoundText = 'Could not find the requested document in the cache'

    if the_page.find(unfoundText) < 0:
        return True
    else:
        return False

def handler(req):
    hdlr = BingHandler('bing')
    return basehandler(req, hdlr)
